local zhongliu = fk.CreateSkill{
  name = "lingling__zhongliu",
}

Fk:loadTranslationTable{
  ["lingling__zhongliu"] = "中流",
  [":lingling__zhongliu"] = "出牌阶段限一次，你可以亮出牌堆顶七张牌，令一名对你造成过伤害的其他角色弃置其中两张牌，再令一名未对你造成过伤害的其他角色弃置其中两张牌，没有符合条件的其他角色则跳过，然后你从剩余牌中获得三张牌。",

  ["#lingling__zhongliu"] = "中流：亮出牌堆顶七张牌，然后获得其中三张牌",

  ["#lingling__zhongliu-damaged"] = "中流：你须令一名对你造成过伤害的其他角色弃置其中两张牌",
  ["#lingling__zhongliu-undamaged"] = "中流：你须令一名未对你造成过伤害的其他角色弃置其中两张牌",
}

zhongliu:addAcquireEffect(function (self, player)
  local room = player.room
  local enemies = {}
  room.logic:getActualDamageEvents(
    999,
    function(e)
      local damageData = e.data
      if damageData.from and damageData.to == player then
        table.insertIfNeed(enemies, damageData.from.id)
      end

      return false
    end,
    Player.HistoryGame
  )
  room:setPlayerMark(player, "lingling__zhongliu_enemy", enemies)
end)

zhongliu:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#lingling__zhongliu",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = room:getNCards(7)
    local damaged = table.map(player:getTableMark("lingling__zhongliu_enemy"), Util.Id2PlayerMapper)
    local undamaged = {}
    for _, p in ipairs(room:getOtherPlayers(player, false)) do
      if not table.contains(damaged, p) then
        table.insert(undamaged, p)
      end
    end
    room:turnOverCardsFromDrawPile(player, cards, zhongliu.name)
    if #damaged > 0 then
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = damaged,
        skill_name = zhongliu.name,
        prompt = "#lingling__zhongliu-damaged",
        cancelable = false
      })[1]
      local c = room:askToChooseCards(to, {
        target = player,
        min = 2,
        max = 2,
        flag = {card_data = {
          { zhongliu.name, cards }
        }},
        skill_name = zhongliu.name,
      })
      for _, v in ipairs(c) do
        table.removeOne(cards, v)
      end
      room:sendFootnote(c, {
        type = "$$DiscardCards",
        from = to.id
      })
      room:cleanProcessingArea(c, zhongliu.name)
    end
    if #undamaged > 0 then
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = undamaged,
        skill_name = zhongliu.name,
        prompt = "#lingling__zhongliu-undamaged",
        cancelable = false
      })[1]
      local c = room:askToChooseCards(to, {
        target = player,
        min = 2,
        max = 2,
        flag = {card_data = {
          { zhongliu.name, cards }
        }},
        skill_name = zhongliu.name,
      })
      for _, v in ipairs(c) do
        table.removeOne(cards, v)
      end
      room:sendFootnote(c, {
        type = "$$DiscardCards",
        from = to.id
      })
      room:cleanProcessingArea(c, zhongliu.name)
    end
    local c = room:askToChooseCards(player, {
      target = player,
      min = 3,
      max = 3,
      flag = {card_data = {
        { zhongliu.name, cards }
      }},
      skill_name = zhongliu.name,
    })
    room:obtainCard(player, c, true, fk.ReasonJustMove, player, zhongliu.name)
    room:cleanProcessingArea(cards, zhongliu.name)
  end
})
zhongliu:addEffect(fk.BeforeHpChanged, {
  can_refresh = function (self, event, target, player, data)
    return
      target == player and
      player:hasSkill(zhongliu.name, true) and
      data.reason == "damage" and
      data.damageEvent and
      data.damageEvent.from
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addTableMarkIfNeed(player, "lingling__zhongliu_enemy", data.damageEvent.from.id)
  end,
})

return zhongliu
